<template>
  <div class="app-container">

    <!-- 查询和其他操作 -->
    <el-form :inline="true" class="demo-form-inline">
        <el-form-item label="储物柜固定资产码">
          <el-input v-model="listQuery.fcatbarcode" clearable class="filter-item" style="width: 200px;" placeholder="请输入储物柜固定资产码"/>
        </el-form-item>
      <el-form-item label="序号">
        <el-input v-model.number="listQuery.fseq" clearable class="filter-item" style="width: 200px;" placeholder="请输入序号"/>
        </el-form-item>
      <el-form-item>
      <el-button round size="medium" class="filter-item" :style="{background:buttonColor.lookup,color:buttonColor.color}" icon="el-icon-search" @click="handleFilter">查找</el-button>
<!--
      <el-button round size="medium" class="filter-item" type="success" icon="el-icon-edit" @click="handleCreate">添加</el-button>
-->
      </el-form-item>
      </el-form>

    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="list" stripe element-loading-text="正在查询中。。。" border fit highlight-current-row :header-cell-style="tableColor">
      <el-table-column :index="indexMethod" align="center" label="序" type="index" />
      <el-table-column align="center" label="序号" prop="fseq" />
      <el-table-column align="center" label="储物柜固定资产码" prop="fcatbarcode" />
      <el-table-column align="center" label="操作" class-name="small-padding">
        <template slot-scope="scope">
          <el-button :style="{background:buttonColor.edit,color:buttonColor.color}" round size="small" class="button-margin" @click="handleUpdate(scope.row)">编辑</el-button><!--v-permission="[':tsControlbox:update']" -->
<!--
          <el-button type="danger" round size="small" class="button-margin" @click="handleDelete(scope.row)">删除</el-button>&lt;!&ndash;v-permission="[':tsControlbox:delete']" &ndash;&gt;
-->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <!-- 添加或修改对话框 -->
    <el-dialog width="45%" :close-on-click-modal="false" :visible.sync="dialogFormVisible">
      <template slot="title">
      <div :style="titleColor">{{textMap[dialogStatus]}}</div>
    </template>
      <el-form ref="dataForm" :rules="rules" :model="dataForm" status-icon label-position="left" label-width="100px" style="width: 400px; margin-left:50px;">
        <el-form-item label="固定资产码" prop="fcatbarcode">
          <el-input disabled v-model="dataForm.fcatbarcode"/>
        </el-form-item>
        <el-form-item label="序号" prop="fseq">
          <el-input v-model.number="dataForm.fseq"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :style="{background:buttonColor.cancel,color:buttonColor.colorone}" @click="dialogFormVisible = false">取消</el-button>
        <el-button v-if="dialogStatus=='create'" :style="{background:buttonColor.confirm,color:buttonColor.color}" @click="createData">确定</el-button>
        <el-button v-else :style="{background:buttonColor.confirm,color:buttonColor.color}" @click="updateData">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style>
  .button-margin {
    margin : 2px 0px;
  }
  .el-button+.el-button {
    margin-left: 1px;
  }
</style>

<script>
import { listtsControlbox, createtsControlbox, updatetsControlbox, deletetsControlbox } from '@/api/tsControlbox'
import Pagination from '@/components/Pagination'
import moment from 'moment'
import { tableColor,titleColor,buttonColor } from '@/filters/index'

export default {
  name: 'ITsControlbox',
  components: { Pagination },
  data() {
    return {
      buttonColor:buttonColor,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        fcatbarcode: undefined,
        fseq: undefined,
        fupddate: undefined,
        foperator: undefined,
        sort: 'create_time',
        order: 'desc'
      },
      dataForm: {
        fcatbarcode: undefined,
        fseq: undefined,
        fupddate: undefined,
        foperator: undefined,
        id: undefined
      },
      tableColor: tableColor,
      titleColor: titleColor,
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '创建'
      },
      rules: {
        fcatbarcode: [
          { required: true, message: '储物柜固定资产码不能为空' },
        ],
        fseq: [
          { type: 'number', message: '序号必须为数字值' },
        ],
        fupddate: [],
        foperator: [],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    datetimeFormat(row, column) {
      const date = row[column.property]
      if (date === undefined || date === null) {
        return ''
      }
      return moment(date).format('YYYY-MM-DD HH:mm:ss')
    },
    getList() {
      this.listLoading = true
      listtsControlbox(this.listQuery)
        .then(response => {
          this.list = response.data.data.list
          this.total = response.data.data.total
          this.listLoading = false
        })
        .catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    resetForm() {
      this.dataForm = {
        fcatbarcode: undefined,
        fseq: undefined,
        fupddate: undefined,
        foperator: undefined,
        id: undefined
      }
    },
    handleCreate() {
      this.resetForm()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          createtsControlbox(this.dataForm)
            .then(response => {
              this.list.unshift(response.data.data)
              this.dialogFormVisible = false
              this.$notify.success({
                title: '成功',
                message: '添加控制柜+柜资料成功'
              })
            })
            .catch(response => {
              this.$notify.error({
                title: '失败',
                message: response.data.data
              })
            })
        }
      })
    },
    handleUpdate(row) {
      this.dataForm = Object.assign({}, row)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          updatetsControlbox(this.dataForm, this.$route.query.id)
            .then(() => {
              for (const v of this.list) {
                if (v.id === this.dataForm.id) {
                  const index = this.list.indexOf(v)
                  this.list.splice(index, 1, this.dataForm)
                  break
                }
              }
              this.dialogFormVisible = false
              this.$notify.success({
                title: '成功',
                message: '更新控制柜+柜资料成功'
              })
            })
            .catch(response => {
              this.$notify.error({
                title: '失败',
                message: response.data.data
              })
            })
        }
      })
    },
    handleDelete(row) {
      this.$confirm('是否要删除控制柜+柜资料', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deletetsControlbox(row)
          .then(response => {
            this.$notify.success({
              title: '成功',
              message: '删除控制柜+柜资料成功'
            })
            const index = this.list.indexOf(row)
            this.list.splice(index, 1)
          })
          .catch(response => {
            this.$notify.error({
              title: '失败',
              message: response.data.data
            })
          })
      })
    },
    indexMethod(index) {
      index = index + 1
      return index
    }
  }
}
</script>
